package com.jia.acl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jia.acl.entity.Permission;
import com.jia.acl.entity.RolePermission;
import com.jia.acl.entity.User;
import com.jia.acl.mapper.UserMapper;
import com.jia.acl.service.*;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author dujiajia
 * @since 2021-02-03
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    RolePermissionService rolePermissionService;

    @Autowired
    PermissionService permissionService;

    @Override
    public List<Permission> selectPermissionByUsername(String username) {
        // 1 根据用户名查询是否有用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = baseMapper.selectOne(wrapper);
        if (user == null){
            return null;
        }
        Long userId = user.getId();
        // 根据用户id查询用户的角色id
        Long roleId = userRoleService.selectRoleByUserId(userId);
        // 根据角色roleId获取获取权限id集合
        List<Long> permissionIds= rolePermissionService.selectPermissionListByRoleId(roleId);
        // 根据 权限id 集合 获取用户权限
        List<Permission> permissionList =  permissionService.selectByIds(permissionIds);
        return permissionList;

    }

    @Override
    public User selectByUsername(String username) {
        // 1 根据用户名查询是否有用户
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("username", username);
        User user = baseMapper.selectOne(wrapper);
        return user;
    }
}
